package com.hns.can.master.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysCodeDscEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.base.vo.SysCodeDscVo;
import com.hns.can.common.constants.GlobalConst;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;

/**
 * @版权：HNS 版权所有 (c) 2013
 * @author:Fisher
 * @version Revision 1.0.0
 * @email:zhuangcaijin@126.com
 * @see:
 * @创建日期：2014-6-19
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人 修改内容
 * @2014-6-19 Fisher 创建
 * @end
 */
@Service
public class SysCodeDscService extends CommonService {

    
    /**
     * @param corpId
     * @return
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-12-29
     * @功能说明：获取需要维护的代码描述的类别名称
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<SysCodeDscEntity> getSysCodeDscCategoryName(String corpId) {
        List<SysCodeDscEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("sysCodeDsc.getSysCodeDscCategoryName", corpId), SysCodeDscEntity.class);
        return list;
    }
    
    /**
     * @param paraMap
     * @return
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-12-29
     * @功能说明：获取对应类别下的代码描述信息
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<SysCodeDscEntity> searchSysCodeDscByCodeClId(Map<String, Object> paraMap) {
        List<SysCodeDscEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("sysCodeDsc.searchSysCodeDscByCodeClId", paraMap), SysCodeDscEntity.class);
        return list;
    }
    
    /**
     * @param request
     * @param sysCodeDscVo
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-12-30
     * @功能说明：保存代码描述信息
     */
    @CacheEvict(value=GlobalConst.CACHE_KEY_SYS_CODE, key="#user.getCorpId()")
    @Transactional
    public void uptSysCodeDsc(SysUserEntity user,SysCodeDscVo sysCodeDscVo) {
        String corpId = sysCodeDscVo.getCorpId();
        String codeClId = sysCodeDscVo.getCodeClId();
        String codeClDsc = sysCodeDscVo.getCodeClDsc();
        List<String> codeValues = sysCodeDscVo.getCodeValues();
        List<String> codeDscs = sysCodeDscVo.getCodeDscs();
        List<String> wthrDefaults = sysCodeDscVo.getWthrDefaults();
        List<String> enabledSts = sysCodeDscVo.getEnabledSts();
        
        // 检查代码描述是否有个性定制
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("corpId", corpId);
        paraMap.put("qcodeClId", codeClId);
        Integer count = (Integer) sqlMapDao.selectOne("sysCodeDsc.checkSysCodeDscExist", paraMap);
        if (count > 0) { // 有个性定制，删除corpId下的数据
            this.sqlMapDao.delete("sysCodeDsc.deleteSysCodeDsc", paraMap);
        }
        // 保存代码描述信息
        for (int i = 0; i < codeValues.size(); i++) {
            SysCodeDscEntity entity = new SysCodeDscEntity();
            entity.setCodeClDsc(codeClDsc);
            entity.setCodeClId(codeClId);
            entity.setCodeDsc(codeDscs.get(i));
            entity.setCodeValue(codeValues.get(i));
            entity.setCorpId(corpId);
            entity.setCrtTime(new Date());
            entity.setCrtUser(user.getAcctName());
            entity.setEnabledSt(enabledSts.get(i));
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(user.getAcctName());
            entity.setWthrDefault(wthrDefaults.get(i));
            entity.setWthrMaintain("1");
            
            this.sqlMapDao.insert("sysCodeDsc.insertSysCodeDsc", entity);
        }
    }
    
}
